/**
 * @author Tobias Boehm
 *
 * 19.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.client.ClientJoinOrLeaveServerRequestMessage;

/**
 * This message should be used to reply to an incoming 
 * {@link ClientJoinOrLeaveServerRequestMessage}.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerJoinOrLeaveServerReplyMessage extends SecureServerMessage implements DefaultTBSserverMessageFlags, ICopyable<ServerJoinOrLeaveServerReplyMessage>  {

	public static enum Reason{OK,GOODBYE,WRONG_PASSWORD,BANNED,ALREADY_JOINED};
	
	private Reason mReason = Reason.OK;
	private boolean mAccepted = false;
	private boolean mEnforceNewPlayerID = false;
	private String mNewPlayerID = "";
	
	
	public ServerJoinOrLeaveServerReplyMessage(){
		
	}
	
	public ServerJoinOrLeaveServerReplyMessage(final boolean pAccepted, final Reason pReason){
		
		this(pAccepted,pReason,false,"");
	}
	
	public ServerJoinOrLeaveServerReplyMessage(final boolean pAccepted, final Reason pReason, final boolean pEnforceNewPlayerID, final String pNewPlayerID){
		
		this.mAccepted = pAccepted;
		
		this.mReason = pReason;
		
		this.mEnforceNewPlayerID = pEnforceNewPlayerID;
		
		this.mNewPlayerID = pNewPlayerID;
	}
	
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_JOIN_OR_LEAVE_SERVER_REPLY;
	}

	@Override
	protected void onReadDecryptedTransmissionData(
			DataInputStream pDataInputStream) throws IOException {

		this.mAccepted = pDataInputStream.readBoolean();
		
		this.mReason = Reason.valueOf(pDataInputStream.readUTF());
		
		this.mEnforceNewPlayerID = pDataInputStream.readBoolean();
		
		this.mNewPlayerID = pDataInputStream.readUTF();
	}
		
	@Override
	protected void onWriteEncryptedTransmissionData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeBoolean(this.mAccepted);
		
		pDataOutputStream.writeUTF(this.mReason.name());
		
		pDataOutputStream.writeBoolean(this.mEnforceNewPlayerID);
		
		pDataOutputStream.writeUTF(this.mNewPlayerID);
	}
	
	/**
	 * @return the mReason
	 */
	public Reason getReason() {
		return mReason;
	}

	/**
	 * @param mReason the mReason to set
	 */
	public void setReason(Reason mReason) {
		this.mReason = mReason;
	}

	/**
	 * @return the mAccepted
	 */
	public boolean isAccepted() {
		return mAccepted;
	}

	/**
	 * @param mAccepted the mAccepted to set
	 */
	public void setAccepted(boolean mAccepted) {
		this.mAccepted = mAccepted;
	}

	/**
	 * @return the mEnforceNewPlayerID
	 */
	public boolean isEnforceNewPlayerID() {
		return mEnforceNewPlayerID;
	}

	/**
	 * @param mEnforceNewPlayerID the mEnforceNewPlayerID to set
	 */
	public void setEnforceNewPlayerID(boolean mEnforceNewPlayerID) {
		this.mEnforceNewPlayerID = mEnforceNewPlayerID;
	}

	/**
	 * @return the mNewPlayerID
	 */
	public String getNewPlayerID() {
		return mNewPlayerID;
	}

	/**
	 * @param mNewPlayerID the mNewPlayerID to set
	 */
	public void setNewPlayerID(String mNewPlayerID) {
		this.mNewPlayerID = mNewPlayerID;
	}

	@Override
	public ServerJoinOrLeaveServerReplyMessage copy() {
		
		final ServerJoinOrLeaveServerReplyMessage copy = new ServerJoinOrLeaveServerReplyMessage();
		
		copy.mAccepted = this.mAccepted;
		copy.mEnforceNewPlayerID = this.mEnforceNewPlayerID;
		copy.mNewPlayerID = this.mNewPlayerID;
		copy.mReason = this.mReason;
		
		return copy;
	}
}
